//
//  MYGMapPositionController.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/14.
//

import UIKit

class MYGMapPositionController: MYGBaseController {
    // MARK: - Property
    let coor: CLLocationCoordinate2D
    
    init(coor: CLLocationCoordinate2D) {
        self.coor = coor
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        configUI()
    }
    
    private func configUI() {
        title = "显示位置"
        
        view.addSubview(mapView)
        
        mapView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        
        let annotation = BMKPointAnnotation()
        annotation.coordinate = coor
        mapView.addAnnotation(annotation)
    }
    
    // MARK: - lazy load
    private lazy var mapView: BMKMapView = {
        let mapV = BMKMapView()
        mapV.centerCoordinate = coor
        mapV.zoomLevel = 15
        mapV.delegate = self
        return mapV
    }()
}

extension MYGMapPositionController: BMKMapViewDelegate {
    func mapView(_ mapView: BMKMapView, viewFor annotation: any BMKAnnotation) -> BMKAnnotationView? {
        if let ann = annotation as? BMKPointAnnotation {
            var annView = mapView.dequeueReusableAnnotationView(withIdentifier: "ann")
            if annView == nil {
                annView = BMKAnnotationView(annotation: annotation, reuseIdentifier: "cell")
            }
            
            annView?.image = UIImage(named: "Map/location")
            
            return annView
        }
        
        return nil
    }
}
